//
//  PostsToolView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/26.
//  Copyright © 2017年 maopu. All rights reserved.
//  发帖工具view

import UIKit

protocol PostsToolViewDelegate:class {
    func postsToolView(_ postsToolView:PostsToolView , didButton button:UIButton)
}

class PostsToolView: UIView {
    fileprivate var emojiBtn:UIButton!
    fileprivate var topView:UIView!
    fileprivate var showCheckImgBtn:UIButton!
    fileprivate var takePhotosBtn:UIButton!
    fileprivate var videoBtn:UIButton!
    
    var maxSelectableCount:Int = 9
    var assetType:DKImagePickerControllerAssetType = .allPhotos
    var assets:[DKAsset] = []

    init(isShowVideo:Bool , frame:CGRect) {
        super.init(frame: frame)
        self.initView(isShowVideo)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initView(_ isShowVideo:Bool){
        self.backgroundColor = UIColor.white
        
        self.topView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 50))
        self.topView.backgroundColor = UIColor.white
        self.addSubview(self.topView)
        
        let topLine = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.8))
        topLine.backgroundColor = UIColor.m_lineColor()
        self.topView.addSubview(topLine)
        
        self.emojiBtn = UIButton.init(type: .custom)
        self.emojiBtn.addTarget(self, action: #selector(self.showEmojisClick(_:)), for: .touchUpInside)
        self.emojiBtn.frame = CGRect.init(x: 0 , y: 5, width: 40, height: 40)
        self.emojiBtn.setImage(UIImage.init(named: "chooseEmoji_ico"), for: UIControlState())
        self.emojiBtn.imageView?.contentMode = .scaleToFill
        self.topView.addSubview(self.emojiBtn)
        
        self.showCheckImgBtn = UIButton.init(type: .custom)
        self.showCheckImgBtn.tag = 1
        self.showCheckImgBtn.addTarget(self, action: #selector(self.buttonClick(_:)), for: .touchUpInside)
        self.showCheckImgBtn.frame = CGRect.init(x: 45 , y: 5, width: 40, height: 40)
        self.showCheckImgBtn.setImage(UIImage.init(named: "chooseImg_ico"), for: UIControlState())
        self.showCheckImgBtn.imageView?.contentMode = .scaleToFill
        self.topView.addSubview(self.showCheckImgBtn)
        
        self.takePhotosBtn = UIButton.init(type: .custom)
        self.takePhotosBtn.tag = 2
        self.takePhotosBtn.addTarget(self, action: #selector(self.buttonClick(_:)), for: .touchUpInside)
        self.takePhotosBtn.frame = CGRect.init(x: self.showCheckImgBtn.frame.maxX + 5 , y: 5, width: 40, height: 40)
        self.takePhotosBtn.setImage(UIImage.init(named: "takePhotos_mini_ico"), for: UIControlState())
        self.takePhotosBtn.imageView?.contentMode = .scaleToFill
        self.topView.addSubview(self.takePhotosBtn)
        
        if !isShowVideo{
            self.videoBtn = UIButton.init(type: .custom)
            self.videoBtn.tag = 3
            self.videoBtn.addTarget(self, action: #selector(self.buttonClick(_:)), for: .touchUpInside)
            self.videoBtn.frame = CGRect.init(x: self.takePhotosBtn.frame.maxX + 5 , y: 5, width: 40, height: 40)
            self.videoBtn.setImage(UIImage.init(named: "video_mini_ico"), for: UIControlState())
            self.videoBtn.imageView?.contentMode = .scaleToFill
            self.topView.addSubview(self.videoBtn)
        }
    }
    
    @objc fileprivate func showEmojisClick(_ button:UIButton){
        
    }
    
    @objc fileprivate func buttonClick(_ button:UIButton){
        if button.tag  == 1{
            Log.shared.printLog("选择图片")
            DKImagePickerUtil.showDKImagePicker(vc: self.findController(), maxSelectableCount: self.maxSelectableCount, assetType: self.assetType, defaultSelectedAssets: self.assets, didSelectAssets: { (assets) in
                self.assets = assets
                
            })
        }else if button.tag  == 2{
            
            Log.shared.printLog("拍照")
        }else if button.tag  == 3{
            Log.shared.printLog("选择视频")
            DKImagePickerUtil.showDKImagePicker(vc: self.findController(), maxSelectableCount: 1, assetType: .allVideos, defaultSelectedAssets: self.assets, didSelectAssets: { (assets) in
                self.assets = assets
                
            })
        }
    }
    
}
